package cn.miao.exception;

import cn.hutool.http.HttpStatus;
import cn.miao.util.AjaxResult;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.AccountExpiredException;
import javax.validation.ConstraintViolationException;
import java.net.BindException;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 全局异常处理器
 *
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 自定义业务异常
     */
    @ExceptionHandler(MiaoRuntimeException.class)
    public AjaxResult businessException(MiaoRuntimeException e) {
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }
    /**
     * 断言异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public AjaxResult illegalArgumentException(IllegalArgumentException e) {
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e) {
        log.error("出现其他异常情况:"+e.getMessage(),e);
        return AjaxResult.me().setSuccess(false).setMessage(ErrorCode.CODE_103_SYSTEM_EXCEPTION.getCodeAndMessage());
    }


    /**
     * 处理空指针异常
     *
     * @param e e
     * @return {@link AjaxResult}
     */
    @ExceptionHandler(NullPointerException.class)
    public AjaxResult handleException(NullPointerException e) {
        log.error(e.getMessage(), e);
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public AjaxResult validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
//        String message = e.getAllErrors().get(0).getDefaultMessage();
        return AjaxResult.me();
    }

    /**
     * 自定义验证异常-@Validated和@Valid 类里面使用
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult validExceptionHandler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        StringJoiner joiner = new StringJoiner("、");
        e.getBindingResult().getAllErrors().forEach(x->joiner.add(x.getDefaultMessage()));
        return AjaxResult.me().setSuccess(false).setMessage(joiner.toString());
    }

    /**
     * 自定义验证异常-JSR303异常-接口上的验证
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public AjaxResult validExceptionHandler(ConstraintViolationException e ) {
        log.error(e.getMessage(), e);
        StringJoiner joiner = new StringJoiner("、");
        e.getConstraintViolations().forEach(x-> joiner.add(x.getMessageTemplate()));
        return AjaxResult.me().setSuccess(false).setMessage(joiner.toString());
    }

}